#pragma once
#include<iostream>
#include"worker.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"
//文件操作
#include<fstream>
#define FILENAME "empfile.txt"
using namespace std;//使用标准命名空间

class WorkerManager
{
private:
    
public:
    WorkerManager();

    //菜单展示
    void ShowMenu();
    
    //退出系统
    void ExitSystem();

    //记录职工人数
    int m_EmpNum;

    //职工数组指针
    Worker **m_EmpArray;

    //添加职工
    void Add_Emp();

    //保存文件
    void save();

    //判断文件是否为空 标志
    bool m_FileIsEmpty;

    //统计文件中人数
    int get_EmpNum();

    //初始化职工
    void init_Emp();

    //显示职工
    void Show_Emp();

    //删除职工
    void Del_Emp();

    //判断职工是否存在,存在返回在数组中的位置，不存在返回-1
    int IsExit(int);

    //修改职工数据
    void Mod_Emp();

    //查找职工
    void Find_Emp();

    //按编号排序
    void Sort_Emp();

    //清空文件
    void Clean_File();

    //职员编号唯一
    bool IsIdUnique(int);

    ~WorkerManager();
};

WorkerManager::WorkerManager()
{
    //初始化属性
    ifstream ifs;
    ifs.open(FILENAME, ios::in);//读文件
    // 1.文件不存在
    if(!ifs.is_open())
    {
        // cout << "文件不存在！" << endl;
        this->m_EmpNum = 0;
        this->m_EmpArray = NULL;
        this->m_FileIsEmpty = true;
        ifs.close();
        return;
    }
    //2.文件存在但为空
    char ch;
    ifs >> ch;
    if(ifs.eof())
    {
        // cout << "文件为空！" << endl;
        this->m_EmpNum = 0;
        this->m_EmpArray = NULL;
        this->m_FileIsEmpty = true;
        ifs.close();
        return;
    }
    //3.文件存在且有内容
    int num = get_EmpNum();
    // cout << "现有职工人数为：" << num << endl;
    m_EmpNum = num;
    //开辟空间
    m_EmpArray = new Worker *[m_EmpNum];
    //将文件中的数据存到数组中
    init_Emp();

}

//菜单展示
void WorkerManager::ShowMenu()
{
    cout << "-----------------------------" << endl;
    cout << "-----欢迎使用职工管理系统----" << endl;
    cout << "--------0.退出管理程序-------" << endl;
    cout << "--------1.显示职工信息-------" << endl;
    cout << "--------2.增加职工信息-------" << endl;
    cout << "--------3.删除离职职工-------" << endl;
    cout << "--------4.修改职工信息-------" << endl;
    cout << "--------5.查找职工信息-------" << endl;
    cout << "--------6.按照编号排序-------" << endl;
    cout << "--------7.清除所有数据-------" << endl;
    cout << "-----------------------------" << endl;
    cout << endl;
}

//退出系统
void WorkerManager::ExitSystem()
{
    cout << "欢迎下次使用!" << endl;
    std::cin.get();//好像是会吃掉一个，写两个算了
    std::cin.get();//敲任意键继续
    exit(0);//退出程序
}

//添加职工
void WorkerManager::Add_Emp()
{
    cout << "请输入要添加的职工数量" << endl;
    int addNum = 0;
    cin >> addNum;
    if(addNum>0)
    {
        //计算新空间大小
        int newSize = this->m_EmpNum + addNum;
        //开辟新空间
        Worker **newSpace = new Worker *[newSize];
        //将原来空间下数据，拷贝到新空间下
        if(this->m_EmpArray!=NULL)
        {
            for (int i = 0; i < this->m_EmpNum;i++)
                newSpace[i] = this->m_EmpArray[i];
        }
        //批量添加新数据
        for (int i = 0; i < addNum;i++)
        {
            int id;//职工编号
            string name;//职工姓名
            int dSelect;//部门选择
            cout << "请输入第" << i + 1 << "个新职工的编号：" << endl;
            cin >> id;
            while (IsIdUnique(id) == false)
            {
                cin >> id;
            }
            cout << "请输入该职工的姓名：" << endl;
            cin >> name;
            cout << "请选择该职工的岗位：" << endl;
            cout << "1.普通职工 2.经理 3.总裁" << endl;
            cin >> dSelect;
            while (dSelect != 1 && dSelect != 2 && dSelect != 3)
            {
                cout << "选择错误，请重新选择：" << endl;
                cin >> dSelect;
            }
            Worker *worker = NULL;
            switch (dSelect)
            {
            case 1:
                worker = new Employee(id, name, 1);
                break;
            case 2:
                worker = new Manager(id, name, 2);
                break;
            case 3:
                worker = new Boss(id, name, 3);
                break;
            default:
                break;
            }
            //将创建的职工指针，保存到数组中
            newSpace[this->m_EmpNum + i] = worker;
        }
            //先释放原来的空间
            delete[] this->m_EmpArray;
            //更改新空间指向
            this->m_EmpArray = newSpace;
            //更新职工人数
            this->m_EmpNum = newSize;
            cout << "成功添加" << addNum << "名职工!" << endl;
            //保存数据到文件中
            save();
            this->m_FileIsEmpty = false;
    }
    else
    {
        cout << "输入有误!" << endl;
    }
    std::cin.get();//任意键继续
    std::cin.get();
    // system("clear");
}

//保存文件
void WorkerManager::save()
{
    ofstream ofs;
    ofs.open(FILENAME, ios::out);//写文件
    //将每个人的数据写入文件中
    for (int i = 0; i < m_EmpNum;i++)
    {
        ofs << m_EmpArray[i]->m_Id << " "
            << m_EmpArray[i]->m_Name << " "
            << m_EmpArray[i]->m_DeptId << endl;
    }
    //关闭文件
    ofs.close();
}
    
//统计文件中人数
int WorkerManager::get_EmpNum()
{
    ifstream ifs;
    ifs.open(FILENAME, ios::in);
    int id;
    string name;
    int dId;
    int num = 0;
    while (ifs >> id && ifs >> name && ifs >> dId)
        num++;
    return num;
}

//从文件中数据初始化职工
void WorkerManager::init_Emp()
{
    ifstream ifs;
    ifs.open(FILENAME, ios::in);
    int id;
    string name;
    int dId;
    int index = 0;
    while (ifs >> id && ifs >> name && ifs >> dId)
    {
        Worker *worker = NULL;
        if (dId == 1) // 普通职工
            worker = new Employee(id, name, dId);
        else if (dId == 2)
            worker = new Manager(id, name, dId);
        else
            worker = new Boss(id, name, dId);
        m_EmpArray[index] = worker;
        // worker->showInfo();
        index++;
    }
    ifs.close();
}

//显示职工
void WorkerManager::Show_Emp()
{
    //判断文件是否为空
    if(m_FileIsEmpty)
        cout << "文件不存在或记录为空！" << endl;
    else
        for (int i = 0; i < m_EmpNum; i++)
            m_EmpArray[i]->showInfo();
    std::cin.get();
    std::cin.get();
}

//判断职工是否存在
int WorkerManager::IsExit(int id)
{
    int index = -1;
    for (int i = 0; i < m_EmpNum; i++)
    {
        if(m_EmpArray[i]->m_Id==id)
        {
            //找到
            index = i;
            break;
        }
    }
    return index;
}

//删除职工
void WorkerManager::Del_Emp()
{
    if(m_FileIsEmpty)
        cout << "文件不存在或记录为空！" << endl;
    else
    {
        //按编号删除
        cout << "请输入要删除的职工编号" << endl;
        int id = 0;
        cin >> id;
        int index = IsExit(id);
        if (index == -1)
            cout << "查无此人！" << endl;
        else
        {
            //数据前移
            for (int i = index; i < m_EmpNum - 1; i++)
            {
                m_EmpArray[i] = m_EmpArray[i + 1];
            }
            m_EmpNum--;//更新数组中人员个数
            //同步更新到文件中
            save();
            cout << "删除成功 ！" << endl;
        }
    }
    std::cin.get();
    std::cin.get();
}

//修改职工数据
void WorkerManager::Mod_Emp()
{
    if(m_FileIsEmpty)
        cout << "文件不存在或记录为空！" << endl;
    else
    {
        cout << "请输入要修改的职工编号" << endl;
        int id = 0;
        cin >> id;
        int index = IsExit(id);

        if (index == -1)
                cout << "查无此人！" << endl;
        else
        {
            m_EmpArray[index]->showInfo();
            delete m_EmpArray[index];
            int newid = 0;
            string newname = "";
            int dSelect = 0;
            cout << "请输入新的职工编号：" << endl;
            cin >> newid;
            while (IsIdUnique(newid) == false)
            {
                cin >> newid;
            }
            cout << "请输入新的职工姓名：" << endl;
            cin >> newname;
            cout << "请选择该职工的岗位：" << endl;
            cout << "1.普通职工 2.经理 3.总裁" << endl;
            cin >> dSelect;
            while (dSelect != 1 && dSelect != 2 && dSelect != 3)
            {
                cout << "选择错误，请重新选择：" << endl;
                cin >> dSelect;
            }
            Worker *worker = NULL;
            switch (dSelect)
            {
            case 1:
                worker = new Employee(newid, newname, 1);
                break;
            case 2:
                worker = new Manager(newid, newname, 2);
                break;
            case 3:
                worker = new Boss(newid, newname, 3);
                break;
            default:
                break;
            }
            //更新数组中的数据
            m_EmpArray[index] = worker;
            save();
            cout << "修改成功！" << endl;
        }
    }
    std::cin.get();
    std::cin.get();
}

//查找职工
void WorkerManager::Find_Emp()
{
    cout << "请输入查找方式：1.按职工编号查找 2.按职工姓名查找" << endl;
    int selete = 0;
    cin >> selete;
    if(selete == 1)
    {
        cout << "请输入要查找的职工编号" << endl;
        int id = 0;
        cin >> id;
        int index = IsExit(id);

        if (index == -1)
                cout << "查无此人！" << endl;
        else
            m_EmpArray[index]->showInfo();
    }
    else if(selete == 2)
    {
        cout << "请输入要查找的职工姓名：" << endl;
        string name = "";
        cin >> name;
        //添加一个找到的标志
        bool flag = false;
        for (int i = 0; i < m_EmpNum; i++)
        {
            if(m_EmpArray[i]->m_Name==name)
            {
                m_EmpArray[i]->showInfo();
                flag =true;
            }
        }
        if(flag==false)
            cout << "查无此人！" << endl;
    }
    else
        cout << "输入有误！" << endl;

    std::cin.get();
    std::cin.get();
}

//按编号排序(选择排序)
void WorkerManager::Sort_Emp()
{
    if(m_FileIsEmpty)
    {
        cout << "文件不存在或记录为空！" << endl;
        std::cin.get();
        std::cin.get();
    }
    else
    {
        cout << "请选择排序方式：1.升序  2.降序" << endl;
        int selete = 0;
        cin >> selete;
        for (int i = 0; i < m_EmpNum; i++)
        {
            int MinOrMax = i;
            for (int j = i + 1; j < m_EmpNum; j++)
            {
                if (selete == 1)
                {
                    if (m_EmpArray[MinOrMax]->m_Id > m_EmpArray[j]->m_Id)
                        MinOrMax = j;
                }
                else
                {
                    if (m_EmpArray[MinOrMax]->m_Id < m_EmpArray[j]->m_Id)
                        MinOrMax = j;
                }
            }
            if(i!=MinOrMax)//交换
            {
                Worker *temp = m_EmpArray[i];
                m_EmpArray[i] = m_EmpArray[MinOrMax];
                m_EmpArray[MinOrMax] = temp;
            }
        }
    }
    cout << "排序成功" << endl;
    save();
    Show_Emp();//排序后展示
}

//清空文件
void WorkerManager::Clean_File()
{
    cout << "确定要清空吗？ 1.确定 2.取消" << endl;
    int selete = 0;
    cin >> selete;
    if(selete==1)
    {
        //删除文件后再重新创建（清空）
        ofstream ofs(FILENAME, ios::trunc);
        ofs.close();
        if (m_EmpArray != NULL)
        {
            for (int i = 0; i < m_EmpNum; i++)
            {
                if(m_EmpArray[i]!=NULL)
                    delete m_EmpArray[i];
            }
            delete[] m_EmpArray;
            m_EmpArray = NULL;
            m_EmpNum = 0;
            m_FileIsEmpty = true;
        }
        cout << "已清空！" << endl;
    }
    std::cin.get();
    std::cin.get();
}

//职员编号唯一
bool WorkerManager::IsIdUnique(int id)
{
    bool flag = true;
    for (int i = 0; i < m_EmpNum; i++)
    {
        if(m_EmpArray[i]->m_Id==id)
        {
            cout << "该职员编号已存在！" << endl;
            cout << "请重新输入：" << endl;
            return false;
        }
    }
    return true;
}

WorkerManager::~WorkerManager()
{
    if (m_EmpArray != NULL)
    {
        //删除堆区的每个职工对象
        for (int i = 0; i < m_EmpNum; i++)
        {
            if(m_EmpArray[i]!=NULL)
                delete m_EmpArray[i];
        }
        //删除堆区数组指针
        delete[] m_EmpArray;
        m_EmpArray = NULL;
    }
}

